package com.example.contentclient;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.contentclient.pojo.User;

public class ContentWriteActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ckb_married;
    private String path;
    private TextView tv_txt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_write);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ckb_married = findViewById(R.id.ckb_married);
        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_load).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);
    }
    @SuppressLint("Range")
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_save:
                ContentValues values=new ContentValues();
                values.put(UserInfoContent.USER_NAME,et_name.getText().toString());
                values.put(UserInfoContent.USER_AGE,Integer.parseInt(et_age.getText().toString()));
                values.put(UserInfoContent.USER_HEIGHT,Float.parseFloat(et_height.getText().toString()));
                values.put(UserInfoContent.USER_WEIGHT,Float.parseFloat(et_weight.getText().toString()));
                values.put(UserInfoContent.USER_MARRIED,ckb_married.isChecked());
                getContentResolver().insert(UserInfoContent.CONTENT_URI,values);
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_load:
                Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URI, null, null, null, null);
                if(cursor!=null)
                {
                    while (cursor.moveToNext()) {
                        User info = new User();
                        info.id = cursor.getInt(cursor.getColumnIndex(UserInfoContent._ID));
                        info.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
                        info.age=cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE));
                        info.height = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_HEIGHT));
                        info.weight = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_WEIGHT));
                        info.married = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_MARRIED))==1?true:false;
                        Log.d("tang", info.toString());
                
                    }
                    cursor.close();
                }
                break;
            case R.id.btn_delete:
//                Uri uri= ContentUris.withAppendedId(UserInfoContent.CONTENT_URI,2);
//                int count = getContentResolver().delete(UserInfoContent.CONTENT_URI, null,null);
//                if(count>0)
//                {
//                    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
//                }
                Uri uri=Uri.parse(UserInfoContent.CONTENT_URI.getPath()+"/"+et_name.getText().toString());
                Log.d("tang", UserInfoContent.CONTENT_URI.getPath()+"/"+et_name.getText().toString());
                int count = getContentResolver().delete(UserInfoContent.CONTENT_URI,null,null);
                if(count>0)
                {
                    Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
